<template>
  <div class="AddTable">
    <div>是否启用多规格<el-switch v-model="value">
      </el-switch></div>
    <el-table :data="tableData" border style="width: 100%;margin-top:10px" :header-cell-style=getHeaderCellStyle()
      :row-class-name="rowClassName" v-if="value">
      <el-table-column type="index" width="50" />
      <el-table-column prop="name" label="规格名称" width="400">
        <template slot-scope="scope">
          <el-input v-model="scope.row.name" @input="changeValue" @blur="blurSpecif" />
        </template>
      </el-table-column>

      <el-table-column prop="guige" label="规格值">
        <template slot-scope="scope">
          <div style="display: flex;align-items: center;">
            <div v-for="(item, index) in scope.row.guige" :key="index" style="width: 150px;margin-right: 10px;">
              <el-input v-model="scope.row.guige[index]" @input="changeValue" @blur="blurSpecif">
                <!-- <i slot="prefix" style="float: right;right:0"> <i class="el-icon-circle-plus"></i></i> -->
                <template #suffix>
                  <i class="el-icon-remove" style="margin-top: 11px;" @click="btnDeteleGuige(scope.row, index)"></i>
                </template>
              </el-input>
            </div>
            <i class="el-icon-circle-plus" @click="btnAddGuige(scope.row, index)"></i>
          </div>
        </template>
      </el-table-column>

      <el-table-column prop="address" label="操作" width="100" style="text-align: center;">
        <template slot-scope="scope">
          <span @click="btnDeleteParent(scope.row.index)">删除</span>
        </template>
      </el-table-column>
    </el-table>
    <div @click="btnAddTab()" v-if="value" style="margin: 20px 0;"> + 添加规格</div>

    <el-table :data="listData" border style="width: 100%" :header-cell-style=getHeaderCellStyle()
      :row-class-name="rowClassName">
      <el-table-column type="index" width="50" />
      <el-table-column prop="img" label="产品主图" width="200">
        <template slot-scope="scope">
          <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false"
            :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="scope.row.img" :src="scope.row.img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </template>
      </el-table-column>

      <el-table-column prop="name" label="规格">
        <template slot-scope="scope">
          <el-input v-model="scope.row.name" @input="changeValue" v-if="!value" />
          <span v-else>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="no" label="编码" width="200">
        <template slot-scope="scope">
          <el-input disabled placeholder="根据编号规则自动生成"></el-input>
        </template>
      </el-table-column>

      <el-table-column prop="address" label="操作" width="100" style="text-align: center;">
        <template slot-scope="scope">
          <span @click="btnDelete(scope.row.index)">删除</span>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0;">产品详情图片 （最多只能上传9张图片）</div>
    <el-upload action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card"
      :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>

  </div>
</template>
<script>
export default {
  // 明细表格
  name: 'AddTable',

  components: {

  },
  props: {
    filedList: Array
  },
  data () {
    return {
      tableData: [],
      listData: [],
      listFalse: [],
      value: false,
      dialogImageUrl: '',
      dialogVisible: false
    }
  },

  computed: {


  },
  methods: {
    handleRemove (file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    rowClassName ({ row, rowIndex }) {
      //把每一行的索引放进row
      row.index = rowIndex;
    },
    btnDeleteParent (index) {
      this.tableData.splice(index, 1);
      this.blurSpecif();
    },
    btnDelete (index) {
      this.listData.splice(index, 1);
    },
    blurSpecif () {
      this.listData = [];
      this.listFalse = this.generateCombinations(this.tableData);
      this.listFalse.forEach(item => {
        var itemNew = {
          name: item,
          img: ''
        }
        this.listData.push(itemNew)

      })
      console.log("listFals====", this.listData)
    },
    generateCombinations (data) {
      return data.reduce((acc, item) => {
        let combinations = item.guige.map(guige => `${item.name}:${guige}`);
        if (acc.length === 0) {
          return combinations;
        }
        let newCombinations = [];
        acc.forEach(oldCombination => {
          combinations.forEach(newCombination => {
            newCombinations.push(`${oldCombination};${newCombination}`);
          });
        });
        return newCombinations;
      }, []);
    },
    setData (list) {
      this.listFalse.forEach((item, index) => {
        let itemnew = item;
        if (valIndex == 0) {
          itemnew = item + newStr;
          console.log("listFalse--2222222222----", itemnew);
          this.listFalse[index] = itemnew;
        } else {
          let str = item + newStr
          console.log("listFalse--33333333----", str);
          this.listFalse.push(str)
        }
      });

    },
    btnDeteleGuige (item, index) {
      item.guige.splice(index, 1);
      this.blurSpecif();

    },
    btnAddGuige (item, index) {
      item.guige.push('');

    },
    btnAddTab () {
      let item = {
        name: '',
        guige: [],
      }
      this.tableData.push(item);

    },
    changeValue () {
      this.$forceUpdate()

    },
    addClick () {
      console.log("添加添加111")
      this.$emit('addClick', "add", -1, {})
    },
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  },
}
</script>
<style>
.AddTable {
  padding: 20px 10px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 78px;
  line-height: 78px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 78px;
  display: block;
}
</style>